<?php

namespace App\Models\Order;

use App\Services\MobileApp\AppEnv;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class OrderFlowStatus
 * @package App\Models\Order
 *
 * @mixin Builder
 */

class OrderFlowStatus extends Model
{
    const ORDER_FLOW_STATUS_ON = 1;
    const ORDER_FLOW_STATUS_ID_PENDING_PAYMENT = 1; // 待付款
    const ORDER_FLOW_STATUS_ID_TO_BE_SHIPPED = 2;   // 待发货
    const ORDER_FLOW_STATUS_ID_TO_BE_RECEIVED = 3;  // 待收货
    const ORDER_FLOW_STATUS_ID_ALL_ORDER = 4;       // 全部订单的id
    const ORDER_FLOW_STATUS_ID_TO_BE_EVALUATE = 5;  // 待评价
    const ORDER_FLOW_STATUS_ID_COMPLETE = 6;        // 已完成

    protected $table = 'order_flow_status';
    protected $guarded = [];
    protected $casts = [
        'order_status_ids' => 'json',
    ];

    public $appends = [
        'order_status_id_map'
    ];

    /**
     * @param $order_flow_status_id
     * @return mixed
     */
    public static function getOrderStatusIds($order_flow_status_id)
    {
        $order_flow_status_id = $order_flow_status_id ?? self::ORDER_FLOW_STATUS_ID_ALL_ORDER;

        switch($order_flow_status_id)
        {
            case self::ORDER_FLOW_STATUS_ID_PENDING_PAYMENT: // 待付款

                $order_status_ids = [OrderStatus::ORDER_STATUS_ID_DEFAULT];
                break;
            case self::ORDER_FLOW_STATUS_ID_TO_BE_SHIPPED: // 待发货（根据后台配置的支付成功信息，不用判断物流）

                $order_status_ids = OrderStatus::getOrderStatusIdToBeShipped();
                break;
            case self::ORDER_FLOW_STATUS_ID_TO_BE_RECEIVED: // 待收货(根据后台配置的待收货状态信息，不用判断物流)

                $order_status_ids = OrderStatus::getOrderStatusIdToBeReceived();
                break;
            case self::ORDER_FLOW_STATUS_ID_ALL_ORDER: // 全部订单

                $order_status_ids = OrderStatus::getOrderStatusIdAllOrder();
                break;
            case self::ORDER_FLOW_STATUS_ID_TO_BE_EVALUATE: // 待评价 (处于已完成并且订单没有评价)

                $order_status_ids = OrderStatus::getOrderStatusIdsToBeEvaluate();
                break;
            case self::ORDER_FLOW_STATUS_ID_COMPLETE: // 已完成 (处于已完成并且订单已评价)

                $order_status_ids = OrderStatus::getOrderStatusIdsToBeEvaluate();
                break;
            default: // 全部订单
                $order_status_ids = OrderStatus::getOrderStatusIdAllOrder();
        }

        return $order_status_ids;
    }

    /**
     * @return mixed
     */
    public function getOrderStatusIdMapAttribute()
    {
        $order_flow_status_id = $this->attributes['order_flow_status_id'];

        return self::getOrderStatusIds($order_flow_status_id);
    }

    /**
     * @param $order_status_id
     * @return bool
     */
    public static function checkIsToBeEvaluate($order_status_id)
    {
        $order_status_to_be_evaluate = OrderStatus::getOrderStatusIdsToBeEvaluate();

        $is_can_to_be_evaluate = in_array($order_status_id, $order_status_to_be_evaluate);

        return $is_can_to_be_evaluate;
    }

    /**
     * @param $order_flow_status_id
     * @return mixed|null
     */
    public static function getOrderFlowStatusName($order_flow_status_id)
    {
        $name = (new self)->where('order_flow_status_id', $order_flow_status_id)
            ->where('language_id', AppEnv::languageId())
            ->first()
            ->name ?? null;

        return $name;
    }

}
